package com.gc.server.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.gc.server.entity.*;
import com.gc.server.service.*;
import com.gc.server.vo.TodoVo;
import com.yf.request.SearchParam;
import com.yf.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import java.util.Map;
import com.yf.consts.DaoConst;
import com.yf.response.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 日程Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Mar 10 11:17:45 CST 2020
 */
@RestController
@RequestMapping("/todo")
public class TodoController {
    @Autowired
    private TodoService todoService;

    @Autowired
    private ActRecordService actRecordService;

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private GoodsRecordService goodsRecordService;

    /**
     * 列表日程
     */
    @RequestMapping("/list")
    @RequiresPermissions("todo:list")
    public R list(@RequestBody SearchParam<TodoVo> params) {
        PageHelper.startPage(params.getPage(),params.getLimit()).setOrderBy(params.getOrder());
        List<TodoVo> todoList = todoService.queryListVo(params.getEntity());
        List<ScoreItemEntity> items = itemService.find(null);
        Map scoreItemMap = new HashMap();
        items.forEach(scoreItemEntity -> {
            scoreItemMap.put(scoreItemEntity.getVal(),scoreItemEntity);
        });

        return R.ok(new PageInfo(todoList)).putExtra("scoreItemMap",scoreItemMap);
    }

    @Autowired
    private ScoreItemService itemService;

    /**
     * 下载
     */
    @SysLog("下载")
    @RequestMapping("/down")
    @RequiresPermissions("todo:list")
    public void down(@RequestBody SearchParam<TodoVo> params, HttpServletResponse response) {
        List<TodoVo> list = todoService.queryListVo(params.getEntity());
        todoService.downLoad(response, list);
    }

    /**
     * 查看日程
     */
    @SysLog("查看日程")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("todo:info")
    public R info(@PathVariable("id") Long id) {
        TodoEntity todo = todoService.get(id);
        List<ActRecordEntity> acts =  actRecordService.findByToDo(id);
        List<ScoreEntity> scores = scoreService.findByTodoId(id);
        List<GoodsRecordEntity> goodsList = goodsRecordService.findByToDoId(id);
        return R.ok(todo).putExtra("acts",acts).putExtra("scores",scores).putExtra("goodsList",goodsList);
    }

    /**
     * 保存日程
     */
    @SysLog("保存日程")
    @RequestMapping("/save")
    @RequiresPermissions("todo:save")
    public R save(TodoEntity todo) {
        todoService.save(todo);

        return R.ok(todo);
    }

    /**
     * 修改日程
     */
    @SysLog("修改日程")
    @RequestMapping("/update")
    @RequiresPermissions("todo:update")
    public R update(Long todoId, String[] names, BigDecimal[] points,String[] remarks) {
//        todoService.update(todo);
       TodoEntity todoEntity = scoreService.preScore(todoId,names,points,remarks);
       todoEntity.setAdminName(ShiroUtils.getUserEntity().getUsername());
       todoService.update(todoEntity);
        return R.ok(todoEntity);
    }


    /**
     * 删除日程
     */
    @SysLog("删除日程")
    @RequestMapping("/delete")
    @RequiresPermissions("todo:delete")
    public R delete(Long[]ids) {
        todoService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有日程
     */
    @SysLog("所有日程")
    @RequestMapping("/queryAll")
    public R queryAll (@RequestParam Map<String,Object> params) {

        List<TodoEntity> todoList = todoService.queryList(params);

        return R.ok(todoList);
    }

    /**
    @ModelAttribute
    public SearchParam<TodoEntity> searchParam(SearchParam<TodoEntity> params, TodoEntity todo) {
        if (todo != null) {
            params.setEntity(todo);
        }

        return params;
    }
 **/
    /***********自定义开始***********/
}
